package com.sinodata.bsm.cicp.collector.db2;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.collector.common.SingleSqlCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * SingleSqlCollector.java
 * <p>
 * DB2缓冲池采集器，需要运算生成结果。
 *
 * @author 邓小峰
 * @version 1.0
 * @Date Sep 21, 2009
 */
public class DB2SingleSqlCollector extends AbstractJDBCCollector {
    private static Logger logger = Logger.getLogger(SingleSqlCollector.class);
    private Long[] lastValue = null;
    private long lastTime = -1;

    /**
     *
     */
    public DB2SingleSqlCollector() {
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String dbtype = this.paramMap.get("DBType");
        String dbname = this.paramMap.get("DBName");
        String sql = this.paramMap.get("SQL");
        String sql2 = this.paramMap.get("SQL2");
        String sql1 = "select value as locklist from SYSIBMADM.DBCFG where name='locklist'";
        String[] propMetas = getPropMeta();
        Long[] tmpValues = new Long[13];
        List property = new ArrayList();
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String value = "0";
        String version = "";
        long va = 0L;
        Long property1 = 0L;
        Long property2 = 0L;
        Long property3 = 0L;
        Long property4 = 0L;
        Long property5 = 0L;
        Long property6 = 0L;
        Long property7 = 0L;
        Long property8 = 0L;
        try {
            ResultSet rs = this.getCachedResultSet(dbtype, ip, dbname, sql);
            ResultSet rs1 = this.getCachedResultSet(dbtype, ip, dbname, sql2);
            while (rs1.next()) {
                version = rs1.getObject(4).toString();
            }
            if (version.charAt(0) == '9') {
                ResultSet rs2 = this.getCachedResultSet(dbtype, ip, dbname, sql1);
                while (rs2.next()) {
                    value = rs2.getObject(1).toString();
                    va = Long.parseLong(value);
                }
            }
            List result = new ArrayList();

            while (rs.next()) {
                for (int j = 0; j < 13; j++) {
                    String v = rs.getObject(j + 1).toString();
                    tmpValues[j] = Long.parseLong(v);
                    result.add(v);
                }
            }

            Long[] v = new Long[13];
            long currentTimeMillis = System.currentTimeMillis();
            if (lastTime > 0 && lastValue != null && lastValue.length == tmpValues.length && currentTimeMillis > lastTime) {
                ResPropertyValue propertyValueX = new ResPropertyValue();
                ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
                v[0] = (tmpValues[0] - lastValue[0]);
                v[1] = (tmpValues[1] - lastValue[1]);
                v[2] = (tmpValues[2] - lastValue[2]);
                v[3] = (tmpValues[3] - lastValue[3]);
                v[4] = (tmpValues[4] - lastValue[4]);
                v[5] = (tmpValues[5] - lastValue[5]);
                v[6] = (tmpValues[6] - lastValue[6]);
                v[7] = (tmpValues[7] - lastValue[7]);
                v[8] = (tmpValues[8] - lastValue[8]);
                v[9] = (tmpValues[9] - lastValue[9]);
                v[10] = (tmpValues[10] - lastValue[10]);
                v[11] = (tmpValues[11] - lastValue[11]);
                v[12] = (tmpValues[12] - lastValue[12]);
                if (v[2] + v[3] == 0) {
                    property1 = 100L;
                } else {
                    property1 = (1 - (v[0] + v[1]) / (v[2] + v[3])) * 100;
                }
                if (v[3] == 0) {
                    property2 = 100L;
                } else {
                    property2 = (1 - (v[1] / v[3])) * 100;
                }
                if (v[4] == 0) {
                    property3 = 100L;
                } else {
                    property3 = (1 - (v[5] / v[4])) * 100;
                }
                property4 = v[6];
                if (v[7] == 0) {
                    property5 = 100L;
                } else {
                    property5 = (1 - (v[8] / v[7])) * 100;
                }
                property6 = v[9];
                if (v[10] == 0) {
                    property7 = 100L;
                } else {
                    property7 = (v[11] / v[10]) * 100;
                }
                if (va == 0) {
                    property8 = -1L;
                } else {
                    property8 = (v[12] / (va * 4096)) * 100;
                }
                property.add(property1);
                property.add(property2);
                property.add(property3);
                property.add(property4);
                property.add(property5);
                property.add(property6);
                property.add(property7);
                property.add(property8);
                for (int i = 0; i < propValues.length; i++) {
                    propValues[i] = ResPropertyValue.createPropValue(property.get(i).toString(), propMetas[i]);
                }
                propertyValueX.setValues(propValues);
                values.add(propertyValueX);
            }
            lastTime = currentTimeMillis;
            lastValue = tmpValues;
        } catch (Exception e) {
            logger.error(this.getClass().getName() + ".collect()", e);
            throw new CollectorException(e);
        }
        return values;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "DBType", "DBName", "SQL", "FieldNum", "SQL2", "DBIP" };
    }

    @Override
    public String[] getPropMeta() {
        int count = Integer.parseInt(this.paramMap.get("FieldNum"));
        String[] propMetas = new String[count];
        for (int i = 1; i <= propMetas.length; i++) {
            propMetas[i - 1] = "$" + i;
        }
        return propMetas;
    }

    /**
     * 最多只支持两位
     *
     * @param number
     * @return
     */
    /*    private static String toChineseString(int number) {
            if (number == 0) {
                return "零";
            }
            String chineseString = "";
            int n = number / 10;
            if (n > 0) {
                if (n > 1) {
                    chineseString += toChineseNumber(n);
                }
                chineseString += "十";
            }
            n = number % 10;
            if (n > 0) {
                chineseString += toChineseNumber(n);
            }
            return chineseString;
        }

        private static String toChineseNumber(int number) {
            String[] chineseNumbers = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            return chineseNumbers[number];
        }*/

    /**
     * @param args
     */
    public static void main(String[] args) {

    }

}
